//
//  ViewController.m
//  UI_10_homework_CustomTableViewCell
//
//  Created by li cheng on 16/7/27.
//  Copyright © 2016年 li cheng. All rights reserved.
//
// hah 


#import "ViewController.h"
#import "NewsTableViewCell.h"
#import "UIView+Frame.h"
#import "NewsNSObject.h"
#import "LCCycleImageView.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *arrayOfTitle;
@property (nonatomic, strong) NSMutableArray *arrayOfImageView;
@property (nonatomic, strong) LCCycleImageView *lcCycleImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfImage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    [self GetData];
    [self buildTableView];
}
- (void)buildTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    [self.tableView registerClass:[NewsTableViewCell class] forCellReuseIdentifier:@"pool"];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.lc_width, self.view.lc_height / 3 + self.view.lc_height / 6)];

//    self.lcCycleImageView = [[LCCycleImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.lc_width, self.view.lc_height / 3)];
//    self.lcCycleImageView.backgroundColor = [UIColor purpleColor];
//    [self.lcCycleImageView setImageArray:array];
//    [headView addSubview:_lcCycleImageView];
    
    UIImageView *imageViewOfBunners = [[UIImageView alloc] initWithImage:_arrayOfImage[1]];
    NSLog(@"aaaaaaaa%@", _arrayOfImage);
    imageViewOfBunners.frame = CGRectMake(0, 0, self.view.lc_width, self.view.lc_height / 3);
    [headView addSubview:imageViewOfBunners];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.backgroundColor = [UIColor greenColor];
    button1.frame = CGRectMake(0, self.view.lc_height / 3, self.view.lc_width / 4, self.view.lc_height / 6 - 30);
    [button1 setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    [headView addSubview:button1];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.lc_height / 3 + (self.view.lc_height / 6 - 30), self.view.lc_width / 4, 30)];
    label1.text = @"贷款买车";
    label1.textAlignment = 1;
    [headView addSubview:label1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.backgroundColor = [UIColor redColor];
    button2.frame = CGRectMake(self.view.lc_width / 4, self.view.lc_height / 3, self.view.lc_width / 4, self.view.lc_height / 6 - 30);
    [button2 setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
    [headView addSubview:button2];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.lc_width / 4, self.view.lc_height / 3 + (self.view.lc_height / 6 - 30), self.view.lc_width / 4, 30)];
    label2.text = @"直销车型";
    label2.textAlignment = 1;
    [headView addSubview:label2];

    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.backgroundColor = [UIColor greenColor];
    button3.frame = CGRectMake(self.view.lc_width / 4 * 2, self.view.lc_height / 3, self.view.lc_width / 4, self.view.lc_height / 6 - 30);
    [button3 setTitle:@"dsfsd" forState:UIControlStateNormal];
    [button3 setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [headView addSubview:button3];
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.lc_width / 4 * 2, self.view.lc_height / 3 + (self.view.lc_height / 6 - 30), self.view.lc_width / 4, 30)];
    label3.text = @"低价买车";
    label3.textAlignment = 1;
    [headView addSubview:label3];

    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.backgroundColor = [UIColor redColor];
    button4.frame = CGRectMake(self.view.lc_width / 4 * 3, self.view.lc_height / 3, self.view.lc_width / 4, self.view.lc_height / 6 - 30);
    [button4 setImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
    [headView addSubview:button4];
    self.tableView.tableHeaderView = headView;
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.lc_width / 4 * 3, self.view.lc_height / 3 + (self.view.lc_height / 6 - 30), self.view.lc_width / 4, 30)];
    label4.text = @"二手车";
    label4.textAlignment = 1;
    [headView addSubview:label4];

}
- (void)GetData{
    //获取路径对象
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe1111" ofType:@"plist"];
    NSDictionary *dicOfSource = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@", dicOfSource);
    NSDictionary *dicOfData = [dicOfSource objectForKey:@"data"];
    NSArray *arrayOfCycleImage = [dicOfData objectForKey:@"cycleImage"];
    self.arrayOfTitle = [NSMutableArray array];
    self.arrayOfImageView = [NSMutableArray array];
    for (NSDictionary *dicOfCycleImage in arrayOfCycleImage) {
        NSString *title = [dicOfCycleImage objectForKey:@"title"];
        [self.arrayOfTitle addObject:title];
        NSString *imageView = [dicOfCycleImage objectForKey:@"picCover"];
        [self.arrayOfImageView addObject:imageView];
    }
    self.arrayOfImage = [NSMutableArray array];
    for (NSString *stringName in _arrayOfImageView) {
        //        UIImage *image = [UIImage imageNamed:ss]
        NSURL *url = [NSURL URLWithString:stringName];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        if (image == nil) {
            image = [UIImage imageNamed:@"11"];
            [self.arrayOfImage addObject:image];
        } else {
            [self.arrayOfImage addObject:image];
        }
    }
    [self.lcCycleImageView setImageArray:self.arrayOfImage];
    NSLog(@"+++++++++++++%@", _lcCycleImageView.imageArray);
    NSLog(@"------------%@", self.arrayOfImage);
    
    [self.tableView reloadData];
    
    

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.lc_height / 8;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrayOfImageView.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    NewsNSObject *newsNSObject = [[NewsNSObject alloc] init];
    newsNSObject.imageView = _arrayOfImageView[indexPath.row];
    newsNSObject.label = _arrayOfTitle[indexPath.row];
    cell.newsNSObject = newsNSObject;
    return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
